#include "inputBox.h"
#include "ImpressionistUI.h"

InputBox::InputBox(int x, int y, int w, int h, const char *l): Fl_Group(x,y,w,h,l){
		m_pDoc = NULL;
        int in_w = 0;
        input  = new Fl_Int_Input(x, y, in_w, h);
        input->callback(cb_input_s, (void*)this);
        input->when(FL_WHEN_ENTER_KEY|FL_WHEN_NOT_CHANGED);

        slider = new Fl_Slider(x+in_w, y, w-in_w, h);
        slider->type(1);
        slider->callback(cb_slider_s, (void*)this);

        bounds(1, 10);     // some usable default
        value(5);          // some usable default
        end();             // close the group
	
}
void InputBox::cb_slider(){
	if(m_pDoc) m_pDoc->numExpandedNodes=(int)(slider->value() + 0.5);
	static int recurse = 0;
        if ( recurse ) { 
            return;
        } else {
            recurse = 1;
            char s[80];
            input->value(s);          // pass slider's value to input
            recurse = 0;
        }
		if(m_pDoc) m_pDoc->refresh();
}

void InputBox::cb_slider_s(Fl_Widget *w, void *data){
	((InputBox*)data)->cb_slider();
}

void InputBox::cb_input(){
	    static int recurse = 0;
        if ( recurse ) {
            return;
        } else {
            recurse = 1;
            int val = 0;
            if ( sscanf(input->value(), "%d", &val) != 1 ) {
                val = 0;
            }
            // fprintf(stderr, "SCANF('%s') -> %d\n", input->value(), val);
            slider->value(val);         // pass input's value to slider
            recurse = 0;
        }

}


void InputBox::cb_input_s(Fl_Widget *w, void *data){
	((InputBox*)data)->cb_input();
}

int InputBox::value() {
	return ((int)(slider->value() + 0.5));
}

void InputBox::value(int val)   { slider->value(val); cb_slider(); }
void InputBox::minumum(int val) { slider->minimum(val); }
int  InputBox::minumum()   { return((int)slider->minimum()); }
void InputBox::maximum(int val) { slider->maximum(val); }
int  InputBox::maximum()   { return((int)slider->maximum()); }
void InputBox::bounds(int low, int high) { slider->bounds(low, high); }